package mysql

import (
	"gorm.io/gorm"
	"shop-login-sys/shoprpc/shoprpc"
)

type Product struct {
	gorm.Model
	Name   string  `gorm:"type:varchar(30)"`
	Stock  int     `gorm:"type:int(11)"`
	Price  float64 `gorm:"type:decimal(10,2)"`
	UserId int     `gorm:"type:int(11)"`
}

func (p *Product) ProductAdd(db *gorm.DB) error {
	return db.Debug().Create(p).Error
}
func ProductList(db *gorm.DB, in *shoprpc.ProductListRequest, list []*shoprpc.ProductList) []*shoprpc.ProductList {
	db.Debug().Model(Product{}).Scopes(Paginate(int(in.Page), int(in.Size))).
		Select("products.id AS ProductId",
			"products.`name`",
			"products.stock AS stock",
			"products.price AS price",
			"users.mobile AS Mobile").
		Joins("  LEFT JOIN users ON products.user_id = users.id").
		Find(&list)
	return list
}
